package com.dries.portal.module.friends.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dries.portal.module.user.mapper.UserMapper;
import com.dries.portal.module.friends.entity.UserAttention;
import com.dries.portal.module.friends.enums.AttentionType;
import com.dries.portal.module.friends.mapper.UserAttentionMapper;
import com.dries.portal.module.friends.service.IUserAttentionService;
import com.dries.portal.module.friends.vo.UserAttentionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserAttentionServiceImpl extends ServiceImpl<UserAttentionMapper, UserAttention> implements IUserAttentionService {

    @Resource
    private UserMapper userMapper;

    /**
     * 我的关注列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserAttentionVo> findMyAttentionListById(String userId) {

        // 获取我关注的好友ID
        List<Long> attentionIds = baseMapper.selectList(new QueryWrapper<UserAttention>().lambda()
                .eq(UserAttention::getUserId, userId)
                .eq(UserAttention::getAttStatus, 1)
        ).stream().map(UserAttention::getAttentionId)
                .collect(Collectors.toList());

        if (CollUtil.isNotEmpty(attentionIds)) {
            return userMapper.selectBatchIds(attentionIds).stream().map(e -> {
                UserAttentionVo attentionVo = new UserAttentionVo();
                BeanUtils.copyProperties(e, attentionVo);
                attentionVo.setUserid(String.valueOf(e.getUserid()));
                boolean b = checkFriendById(userId, attentionVo.getUserid());
                if (b) {
                    attentionVo.setType(AttentionType.ALL_ATTENTION.getCode());
                } else attentionVo.setType(AttentionType.ATTENTION.getCode());
                return attentionVo;
            }).collect(Collectors.toList());
        } else {
            return null;
        }
    }

    /**
     * 我的被关注列表
     *
     * @param userId
     */
    @Override
    public List<UserAttentionVo> findAttentionListById(String userId) {
        // 获取关注我的好友ID
        List<Long> userIds = baseMapper.selectList(new QueryWrapper<UserAttention>().lambda()
                .eq(UserAttention::getAttentionId, userId)).stream().map(UserAttention::getUserId)
                .collect(Collectors.toList());

        if (CollUtil.isNotEmpty(userIds)) {
            return userMapper.selectBatchIds(userIds).stream().map(e -> {
                UserAttentionVo attentionVo = new UserAttentionVo();
                BeanUtils.copyProperties(e, attentionVo);
                attentionVo.setUserid(String.valueOf(e.getUserid()));
                boolean b = checkFriendById(userId, attentionVo.getUserid());
                if (b) {
                    attentionVo.setType(AttentionType.ALL_ATTENTION.getCode());
                } else attentionVo.setType(AttentionType.ATTENTION.getCode());
                return attentionVo;
            }).collect(Collectors.toList());
        } else {
            return null;
        }
    }

    @Transactional
    @Override
    public void attentionByUserId(UserAttention userAttention) {

        // 0. 查询关注信息
        UserAttention attention = baseMapper.selectOne(new QueryWrapper<UserAttention>()
                .lambda().eq(UserAttention::getUserId, userAttention.getUserId())
                .eq(UserAttention::getAttentionId, userAttention.getAttentionId())
        );

        if (attention != null) {
            userAttention.setId(attention.getId());
        }
        // 1.保存关注信息
        saveOrUpdate(userAttention);

    }

    /**
     * 判断是否互关
     *
     * @param userId
     * @param attentionId
     */
    @Override
    public boolean checkFriendById(String userId, String attentionId) {

        UserAttention user = baseMapper.selectOne(new QueryWrapper<UserAttention>().lambda()
                .eq(UserAttention::getUserId, userId)
                .eq(UserAttention::getAttentionId, attentionId)
                .eq(UserAttention::getAttStatus, 1)
        );

        UserAttention attention = baseMapper.selectOne(new QueryWrapper<UserAttention>().lambda()
                .eq(UserAttention::getUserId, attentionId)
                .eq(UserAttention::getAttentionId, userId)
                .eq(UserAttention::getAttStatus, 1)
        );

        if (user != null && attention != null) {
            return true;
        } else {
            return false;
        }

    }

}